/*****************************************************************************
* Project:  avr-iic-driver
*
* Copyright (C) 2013 Bill Wu. All rights reserved.
*
* This program is open source software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Alternatively, this program may be distributed and modified under the
* terms of Quantum Leaps commercial licenses, which expressly supersede
* the GNU General Public License and are specifically designed for
* licensees interested in retaining the proprietary status of their code.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* e-mail:               wushangru@student.hlju.edu.cn
*****************************************************************************/

#ifndef IIC_H
#define IIC_H

/** IIC error type  */
enum IIC_ErrorType {
    NOMAL = 0,
    ERR_START,
    ERR_MT_DATA_ACK,
    ERR_MT_SLA_ACK,
    ERR_MR_SLA_ACK,
    ERR_MR_DATA_ACK,
    ERR_MR_DATA_NACK
};

/** IIC clock, SCL frequency */
enum IIC_Frq {
    IIC_100KHz,
    IIC_200KHz,
    IIC_400KHz
};

enum IIC_IntrPullUp {
    PULL_UP,           /**<    enable internal pull up resistor */
    NO_PULL_UP         /**< disenable internal pull up resistor */
};

/** \brief hardware IIC initialization
 *  \note  CPU frequency must higher then 16 * SCL frequency
 *  \param frq: select IIC clock (SCL) frequency level
 *  \param pullUp: select enable internal pull up resistor or not
 */
void IIC_init(enum IIC_Frq frq, enum IIC_IntrPullUp pullUp);

enum IIC_ErrorType IIC_checkErr(void);

uint8_t  IIC_writeByte (uint8_t DevAddr, uint8_t IntAddr, uint8_t Data,
                        uint8_t NeedIntAddr);

uint8_t IIC_writeNBytes(uint8_t DevAddr, uint8_t IntAddr, uint8_t* Data,
                        uint8_t NumOfBytes, uint8_t NeedIntAddr);

uint8_t  IIC_readByte (uint8_t DevAddr, uint8_t IntAddr, uint8_t NeedIntAddr);

uint16_t IIC_read2Bytes(uint8_t DevAddr, uint8_t IntAddr,uint8_t NeedIntAddr);

uint8_t  IIC_readNByte (uint8_t DevAddr, uint8_t IntAddr, uint8_t* Data,
                        uint8_t DataLen, uint8_t NeedIntAddr);


#endif /* IIC_H */

